package com.util.DBpool;

import com.util.GlobalArgs;

import java.sql.*;
import java.util.Vector;

/**
 * 默认的实现池
 */
public class DefaultPool implements PoolIF{

    private Vector<PoolConnection> PoolConnectionVector = new Vector<>();
    private static String DataBase_Url;
    private static String DataBase_User;
    private static String DataBase_Password;
    private static String DataBase_Driven;
    //连接池不足时，增长的步幅
    public static int step;
    //连接池最大容量
    private static int MaxCount;
    //连接池初始大小
    private static int InitCount;
    //初始化
    private void Init() {
        DataBase_Driven = GlobalArgs.DataBase_Driven;
        DataBase_Url = GlobalArgs.DataBase_Url;
        DataBase_User = GlobalArgs.DataBase_User;
        DataBase_Password = GlobalArgs.DataBase_Password;
        step = GlobalArgs.step;
        MaxCount = GlobalArgs.MaxCount;
        InitCount = GlobalArgs.InitCount;
    }

    public DefaultPool() {
        Init();
        try {
            Class.forName(DataBase_Driven);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        createPoolConnection(InitCount);
    }
    /**
     *
     */
    @Override
    public PoolConnection getPoolConnection() {
        if(PoolConnectionVector.size() < 0) {
            throw new RuntimeException("连接池初始化失败");
        }
        PoolConnection poolConnection = null;
        try {
            poolConnection = getPoolConnectionFromPool();
            //连接池已满，按step增加
            if(poolConnection == null) {
                createPoolConnection(step);
                poolConnection = getPoolConnectionFromPool();
                return poolConnection;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return poolConnection;
    }


    //向连接池加入新的连接
    @Override
    public boolean createPoolConnection(int count) {
        if(PoolConnectionVector.size() > MaxCount || PoolConnectionVector.size() + count > MaxCount) {
            System.out.println("Connection pool is full");
            return false;
        }
        for(int i = 0; i < count; i++) {
            try {
                Connection connection = DriverManager.getConnection(DataBase_Url, DataBase_User, DataBase_Password);
                PoolConnection poolConnection = new PoolConnection(connection, false);
                PoolConnectionVector.add(poolConnection);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }


    //遍历连接池取出连接
    private synchronized PoolConnection getPoolConnectionFromPool() throws SQLException{
        for(PoolConnection poolConnection: PoolConnectionVector) {
            if(!poolConnection.getBusy()) {
                System.out.println("Successed getting connection from pool");
                //test
                int count = 0;
                for(PoolConnection temp:PoolConnectionVector){
                    if (temp.getBusy()){
                        count ++;
                    }
                }


                if(poolConnection.getConnection().isValid(3000)) {        //
                    poolConnection.setBusy(true);
                    return poolConnection;
                } else {
                    poolConnection.getConnection().close();             //关闭失效connection
                    //检测链接超时，重新建立connection，更新链接池中链接
                    Connection connection = DriverManager.getConnection(DataBase_Url, DataBase_User, DataBase_Password);
                    poolConnection.setConnection(connection);
                    poolConnection.setBusy(true);
                    return poolConnection;
                }
            }
        }
       //all busy
        return null;
    }


}
